<template>
  <d2-container>
    <div style="margin: 20px">
      <div style="margin: 10px 0">
        <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="service_name"></el-input>
        <el-button style="margin-left: 10px" type="primary" @click="getServices">搜索</el-button>
        <el-button style="margin-left: 10px" type="primary" @click="reset">重置</el-button>
      </div>
      <div style="margin: 10px 0">
        <el-row v-for="item in services" :key="item.id">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
<!--              <span>{{ item.name | nameFilter  }}</span>-->
              <span>{{ item.name }}</span>
              <el-popconfirm popper-class="popper" title="确认删除该服务吗?" @confirm="deleteService(item.id)">
                <el-button style="float: right; margin-left:10px; padding: 5px 0; color: rgba(197,69,102,0.68)"
                           type="text" slot="reference">删除
                </el-button>
              </el-popconfirm>
              <el-popconfirm popper-class="popper" title="确认上线该服务吗?" @confirm="handleUpdate(item)">
                <el-button style="float: right; padding: 5px 0" type="text" slot="reference"
                           :style="{ display: item.status===1?'none':'' }">上线
                </el-button>
              </el-popconfirm>
              <el-popconfirm popper-class="popper" title="确认下线该服务吗?" @confirm="handleUpdate(item)">
                <el-button style="float: right; padding: 5px 0; color: rgba(197,69,102,0.68)" type="text" slot="reference"
                           :style="{ display: item.status===0?'none':'' }">下线
                </el-button>
              </el-popconfirm>
            </div>
            <el-descriptions class="margin-top" :column="3" border>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"/>
                  项目名称
                </template>
                {{ item.project_name }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"/>
                  模型名称
                </template>
<!--                {{ item.model_name | nameFilter  }}-->
                {{ item.model_name }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-mobile-phone"/>
                  服务类型
                </template>
                <el-tag size="small" v-if="item.type===0">
                  图像分类
                </el-tag>
                <el-tag size="small" v-if="item.type===1">
                  目标检测
                </el-tag>
                <el-tag size="small" v-if="item.type===2">
                  图像分割
                </el-tag>
              </el-descriptions-item>
            </el-descriptions>
          </el-card>
        </el-row>
      </div>
      <div style="margin: 10px 0">
        <el-card class="card" v-if="totalPage >= 0">
          <!-- 分页插件 -->
          <div>
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[1, 2, 3]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalPage">
            </el-pagination>
          </div>
        </el-card>
      </div>
    </div>
  </d2-container>
</template>

<script>

import { DelServiceObj, GetServiceList, UpdateServiceObj } from './api'

export default {
  name: 'index',
  data () {
    return {
      services: [],
      service_name: '',
      totalPage: 0,
      currentPage: 1,
      pageSize: 3
    }
  },
  filters: {
    nameFilter (value) {
      return value.substring(0, value.length - 36)
    }
  },
  methods: {
    // 获取所有服务
    getServices () {
      const query = { name: this.project_name, creator: this.$store.state.d2admin.user.info.user_id }
      query.limit = this.pageSize
      query.page = this.currentPage
      GetServiceList(query).then(response => {
        this.totalPage = response.data.total
        this.currentPage = response.data.page
        this.services = response.data.data
      })
    },
    // 重置搜索框
    reset () {
      this.service_name = ''
      this.getServices()
    },
    handleUpdate (item) {
      item.status = Number(!item.status)
      console.log(item)
      UpdateServiceObj(item).then(response => {
        this.$message.success('服务状态更新成功！')
        this.getServices()
      }).catch(error => {
        this.$message.error('服务状态更新失败！')
        console.log(error)
      })
    },
    // 删除服务
    deleteService (id) {
      DelServiceObj(id).then(response => {
        this.$message.success('服务删除成功！')
        this.getServices()
      }).catch(error => {
        this.$message.error('服务删除失败！')
        console.log(error)
      })
    },
    // 每页条数改变
    handleSizeChange (val) {
      this.pageSize = val
      this.getServices()
    },
    // 翻页
    handleCurrentChange (val) {
      this.currentPage = val
      this.getServices()
    }
  },
  created: function () {
    this.getServices()
  }

}
</script>

<style scoped>
</style>
